package team.kirohuji.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import team.kirohuji.dao.SystemRoleDao;
import team.kirohuji.dao.SystemUserDao;
import team.kirohuji.dao.SystemUserRoleDao;
import team.kirohuji.domain.SystemRole;
import team.kirohuji.domain.SystemUser;
import team.kirohuji.service.SystemUserRoleService;
import team.kirohuji.service.SystemUserService;
import team.kirohuji.utils.ShiroUtils;
/**
 * 系统用户服务实现类
 * @author kirohuji
 *
 */
@Service("systemUserService")
public class SystemUserServiceImpl implements SystemUserService {

    /**
     * @see SystemUserDao
     * systemUserDao
     */
    @Resource
    private SystemUserDao systemUserDao;
    /**
     * @see SystemRoleDao
     * systemRoleDao
     */
    @Resource
    private SystemRoleDao systemRoleDao;
    /**
     * @see SystemUserRoleDao
     * systemUserRoleDao
     */
    @Resource
    private SystemUserRoleService systemUserRoleService;


    /* 
     * 查询所有权限
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryAllPerms(java.lang.Integer)
     * @param userId 用户主键
     * @return String列表
     */
    @Override
    public List<String> queryAllPerms(Integer userId) {

        return systemUserDao.queryAllPerms(userId);
    }

    /* 
     * 查询用户所拥有全部菜单Id
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryAllMenuId(java.lang.Integer)
     * @param userId 用户主键
     * @return Integer列表
     */
    @Override
    public List<Integer> queryAllMenuId(Integer userId) {

        return systemUserDao.queryAllMenuId(userId);
    }

    /* 
     * 查询一条记录通过登录名
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryByLoginName(java.lang.String)
     * @param username 用户名
     * @return SystemUser
     */
    @Override
    public SystemUser queryByLoginName(String username) {

        return systemUserDao.queryByLoginName(username);
    }

    /* 
     * 查询一条记录通过用户Id
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryObject(java.lang.Integer)
     * @param userId 用户主键
     * @return SystemUser
     */
    @Override
    public SystemUser queryObject(Integer userId) {

        return systemUserDao.queryObject(userId);
    }

    /* 
     * 查询一组记录
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryList(java.util.Map)
     * @param map map对象
     * @return SystemUser列表
     */
    @Override
    public List<SystemUser> queryList(Map<String, Object> map) {

        return systemUserDao.queryList(map);
    }

    /* 
     * 查询全部
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryTotal(java.util.Map)
     * @param map map对象
     * @return int
     */
    @Override
    public int queryTotal(Map<String, Object> map) {

        return systemUserDao.queryTotal(map);
    }

    /* 
     * 存入一条记录
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#save(team.kirohuji.domain.SystemUser)
     * @param user 用户
     * @throws IllegalArgumentException 
     */
    @Override
    @Transactional
    public void save(SystemUser user) throws IllegalArgumentException {

        //先判断 登录名是否存在
        if( systemUserDao.queryByLoginName(user.getLoginName())!=null ){
            throw new IllegalArgumentException("登录名已存在！");
		}
		RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
		
		String salt = randomNumberGenerator.nextBytes().toHex()+user.getLoginName();

        //md5 加密
        user.setPassword(ShiroUtils.MD5(user.getPassword(), salt));
        user.setSalt(salt);//加入干扰字母

        //保存用户
        systemUserDao.save(user);
        //保存用户与角色关系
        systemUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
    }

    /* 
     * 更新
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#update(team.kirohuji.domain.SystemUser)
     * @param user 用户
     * @throws IllegalArgumentException
     */
    @Override
    @Transactional
    public void update(SystemUser user) throws IllegalArgumentException {
		if(StringUtils.isBlank(user.getPassword())){
			user.setPassword(null);
		}else{
			RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
			String salt = randomNumberGenerator.nextBytes().toHex()+user.getLoginName();
			//md5 加密
			user.setPassword(ShiroUtils.MD5(user.getPassword(), salt));
			user.setSalt(salt);//加入干扰字母
		}
		
		systemUserDao.update(user);

        //保存用户与角色关系
		//systemUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
    }

    /* 
     * 批量删除
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#deleteBatch(java.lang.Integer[])
     * @param userIds 用户主键数组
     */
    @Override
    @Transactional
    public void deleteBatch(Integer[] userIds) {
        systemUserDao.deleteBatch(userIds);
    }

    /* 
     * 更新密码
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#updatePassword(java.lang.Integer, java.lang.String, java.lang.String)
     * @param userId 用户主键
     * @param password 密码
     * @param newPassword 新密码
     * @return int
     */
    @Override
    public int updatePassword(Integer userId, String password, String newPassword) {

        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassword);

        return systemUserDao.updatePassword(map);
    }
    /* 
     * 查询所有用户
     * (non-Javadoc)
     * @see team.kirohuji.service.SystemUserService#queryAllUser()
     * @return SystemUser列表
     */
    @Override
    public List<SystemUser> queryAllUser() {
        return systemUserDao.queryAllUser();
    }
}